package com.longrise.android.byjk.plugins.productfeerate;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.common.utils.AppUtil;

/**
 * Created by callxkj on 2017/6/6.
 * 上传产品费率的dialogutil
 */

public class ProductfeeDialogHelper {

    private static final ProductfeeDialogHelper mDialogHelper = new ProductfeeDialogHelper();
    private Dialog mFailDialog;
    private Dialog mProgressDialog;
    private Dialog mSuccessDialog;


    private ProductfeeDialogHelper() {

    }

    public static ProductfeeDialogHelper getInstance() {
        return mDialogHelper;

    }

    /**
     * 进度的dialog
     */
    public Dialog createProgressDialog(Context context, View view, int width, int height) {
        dismissProgressDialog();
        mProgressDialog = new Dialog(context, R.style.VideoListDialog);
        width = AppUtil.dip2px(width);
        height = AppUtil.dip2px(height);
        mProgressDialog.getWindow().setContentView(view, new ViewGroup.LayoutParams(width, height));
        mProgressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        return mProgressDialog;
    }

    /**
     * 上传成功的dialog
     */
    public Dialog createSuccessDialog(Context context) {
        dismissSuccessDialog();
        final View successview = View.inflate(context, R.layout.dialog_productfee_upload_end, null);
        final TextView tv_tips = (TextView) successview.findViewById(R.id.dialog_productfee_tips);
        tv_tips.setText("文件上传成功，感谢您的等待。");
        mSuccessDialog = new Dialog(context, R.style.VideoListDialog);
        int width = AppUtil.dip2px(270);
        int height = AppUtil.dip2px(72);
        mSuccessDialog.getWindow().setContentView(successview, new ViewGroup.LayoutParams(width, height));
        mSuccessDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mSuccessDialog.setCanceledOnTouchOutside(false);
        mSuccessDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        return mSuccessDialog;
    }


    /**
     * 显示上传结束的dialog
     */
    public Dialog createFailDialog(Context context) {
        dismissFailDialog();
        View failview = View.inflate(context, R.layout.dialog_productfee_upload_end, null);
        TextView tv_tips2 = (TextView) failview.findViewById(R.id.dialog_productfee_tips);
        tv_tips2.setText("文件上传失败，请重试。");
        mFailDialog = new Dialog(context, R.style.VideoListDialog);
        int width = AppUtil.dip2px(270);
        int height = AppUtil.dip2px(72);
        mFailDialog.getWindow().setContentView(failview, new ViewGroup.LayoutParams(width, height));
        mFailDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mFailDialog.setCanceledOnTouchOutside(true);
        return mFailDialog;
    }


    public void dismissProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    public void dismissSuccessDialog() {
        if (mSuccessDialog != null && mSuccessDialog.isShowing()) {
            mSuccessDialog.dismiss();
            mSuccessDialog = null;
        }
    }

    public void dismissFailDialog() {
        if (mFailDialog != null && mFailDialog.isShowing()) {
            mFailDialog.dismiss();
            mFailDialog = null;
        }
    }

}
